/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se1603.springmvc.web.action;

import com.github.pagehelper.Page;
import edu.fjut.se1603.springmvc.entity.SysUser;
import edu.fjut.se1603.springmvc.service.UserService;
import edu.fjut.se1603.springmvc.util.PageUtil;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author danny
 */
@Controller
@RequestMapping("/user")
public class UserAction {
    
    @Autowired
    UserService userService;
    
    @RequestMapping("/list")
    public String userList(HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelMap model) {
        SysUser user = new SysUser();
        List<SysUser> ul = userService.queryList(user);
//        Page<SysUser> page = (Page<SysUser>) ul;
//        request.setAttribute("ul", ul);
        model.addAttribute("ul", ul);
        return "userList";
    }
    
    @RequestMapping("/add/form")
    public String addForm() {
        return "userAdd";
    }
    
    @RequestMapping("/add/do")
    public String doAdd(SysUser user, Map model) {
        boolean result = false;
        try {
            result = userService.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result)
            return "redirect:/user/list";
        else {
            model.put("error", "添加失败！");
            return "userAdd";
        }
    }
    
    @RequestMapping("/add/doajax")
    public void doAddAjax(SysUser user, Map model) {
        boolean result = false;
        try {
            result = userService.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.put("result", result);
    }
    
    @RequestMapping("/easyuiList")
    public String easyuiList() {
        return "userListEasyui";
    }
    
    @RequestMapping("/listdata")
    public void listData(ModelMap model, PageUtil pageUtil) {
        List<SysUser> ul = userService.getUsersWithPagination(pageUtil.getPage(), pageUtil.getRows());
        Page<SysUser> page = (Page<SysUser>) ul;
        model.addAttribute("rows", page.getResult()).addAttribute("total", page.getTotal());
    }
    
}
